package com.yonyou.cyxdms.sysAuth.service.utill;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;

/**
 * <p>Title: HttpClientUtils</p>
 * <p>Description: httpClient 工具类</p>
 *
 * @author luzizhi
 */
public class HttpClientUtils {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
    /**
     * 默认参数设置
     * setConnectTimeout：设置连接超时时间，单位毫秒。
     * setConnectionRequestTimeout：设置从connect Manager获取Connection 超时时间，单位毫秒。
     * setSocketTimeout：请求获取数据的超时时间，单位毫秒。访问一个接口，多少时间内无法返回数据，就直接放弃此次调用。 暂时定义15分钟
     */
    private RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(600000).setConnectTimeout(600000).setConnectionRequestTimeout(600000).build();

    /**
     * 静态内部类---作用：单例产生类的实例
     *
     * @author Administrator
     */
    private static class LazyHolder {
        private static final HttpClientUtils INSTANCE = new HttpClientUtils();

    }

    private HttpClientUtils() {
    }

    public static HttpClientUtils getInstance() {
        return LazyHolder.INSTANCE;
    }

    /**
     * 发送 get请求
     */
    public String sendHttpGet(String httpUrl) {
        // 创建get请求
        HttpGet httpGet = new HttpGet(httpUrl);
        return sendHttpGet(httpGet);
    }

    /**
     * 发送Get请求
     */
    private String sendHttpGet(HttpGet httpGet) {
        // CloseableHttpClient httpClient = null;
        // CloseableHttpResponse response = null;
        HttpEntity entity;
        String responseContent = null;
        httpGet.setConfig(requestConfig);
        try (CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpGet)) {
            // 创建默认的httpClient实例.
            // 执行请求
            entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            logger.error("发送Get请求失败", e);
        } /*finally {
            try {
                // 关闭连接,释放资源
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
        return responseContent;
    }

    /**
     * 发送Get请求Https
     */
    private String sendHttpsGet(HttpGet httpGet) {
        /*CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;*/
        HttpEntity entity;
        String responseContent = null;
        try {
            // 创建默认的httpClient实例.
            PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString()));
            DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);

            try (CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build(); CloseableHttpResponse response = httpClient.execute(httpGet)) {
                //httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
                httpGet.setConfig(requestConfig);
                // 执行请求
                //response = httpClient.execute(httpGet);
                entity = response.getEntity();
                responseContent = EntityUtils.toString(entity, "UTF-8");
            } catch (Exception e) {
                logger.error("发送Get请求Https失败", e);
            }
        } catch (Exception e) {
            logger.error("发送Get请求Https失败", e);
        }
        return responseContent;
    }
}
